package slotMachineGUI;
import java.net.*;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ShopNameAddress{
	
	private static String keyWord; //要搜尋的關鍵字
	public static String place;
	public static String price;
	//private String address;
	
	public static void ShopNameAddress(String key){ //建構子 初始化要搜尋的關鍵字
		String[] names = key.split(" "); //分割所傳入的字串 此處定義關鍵字分割後結果為 
        //name[0]=價位,name[1]=關鍵字 name[2]=地點
		price = names[0];
		keyWord = names[1];
		place= names[2];
	}
	
	public String getAddress(){ 
		return keyWord;
	}

	public static void start(){
		
		String web="http://www.ipeen.com.tw/search/all/000/0-100-0-0/"+keyWord+"/?adkw="+place+"&bar=1";
		read(web);
	}
	
	
    public static void read(String strURL ){//抓取網頁資料 
    	
        try  { 
    	URL pageUrl = new URL(strURL);
        URLConnection conn = pageUrl.openConnection();
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String ShopName="<a[ ]*href=\"/shop/[0-9]*-(.*)\" class=\"a37\">(.*)</a>"; //店名的Regular expression
        String ShopAddress="<span style=\"padding-left:3em;\">(.*)";//店址的Regular expression
        String ShopPrice="&nbsp;([0-9]+)&nbsp;";//價格的Regular expression
        String ShopImage="<a target=\"_blank\" href=\"/shop/.*\" ><img src=\"(http://.*)\"  alt=\".*";//店家圖片的Regular expression
        String StopSearch="<h3 class=\"type\"><a href=\"/search/all.*>";//額外不相關商品的Regular expression
        
        
        
     
        
        Pattern name= Pattern.compile(ShopName);
        Pattern address= Pattern.compile(ShopAddress);
        Pattern Price= Pattern.compile(ShopPrice);
        Pattern image = Pattern.compile(ShopImage);
        Pattern stop= Pattern.compile(StopSearch);
       
        String line;
        
        File writeFile=new File("ShopList.txt");
        FileWriter fwriter = new FileWriter(writeFile);
       while ((line = reader.readLine()) != null) {
			Matcher matcherName = name.matcher(line);
			Matcher matcherAddress = address.matcher(line);
			Matcher matcherPrice = Price.matcher(line);
			Matcher matcherImage = image.matcher(line);
			Matcher matcherStop = stop.matcher(line);
			
			
			boolean matchNameFound = matcherName.find();
			boolean matchAddressFound = matcherAddress.find();
			boolean matchPriceFound = matcherPrice.find();
			boolean matchImageFound = matcherImage.find();
            boolean matchStopFound = matcherStop.find();
            
            
            if(matchStopFound){
            	break;
            }//停止條件符合
            else{
			while(matchNameFound) {
                  String groupStr = matcherName.group(1);
                  fwriter.write(groupStr+",");//將店名寫入檔案
                if(matcherName.end() + 1 <= line.length()) { 
                	matchNameFound = matcherName.find(matcherName.end());
                }
                else{ 
                	break; 
                }//繼續往下找尋符合店名RE的字串
            }//結束找尋店名
            while(matchAddressFound) {
                String groupStr = matcherAddress.group(1);
                groupStr= groupStr.replaceAll("<em>","");
                groupStr= groupStr.replaceAll("</em>","");
                fwriter.write(groupStr+",");//將地址寫入檔案
              if(matcherAddress.end() + 1 <= line.length()) { 
              	matchAddressFound = matcherAddress.find(matcherAddress.end());
              }
              else{ 
              	break; 
              }//繼續往下找尋符合地址RE的字串
            }//結束找尋地址
            while(matchPriceFound) {
                String groupStr = matcherPrice.group(1);
                fwriter.write(groupStr+"\n");//將價錢寫入檔案
              if(matcherPrice.end() + 1 <= line.length()) { 
              	matchPriceFound = matcherPrice.find(matcherPrice.end());
              }
              else{ 
              	break; 
              }//繼續往下找尋符合價錢RE的字串
            }//結束找尋價錢
            
            while(matchImageFound) {
                String groupStr = matcherImage.group(1);
                fwriter.write(groupStr+",");//將圖片URL寫入檔案
              if(matcherImage.end() + 1 <= line.length()) { 
            	  matchImageFound = matcherImage.find(matcherImage.end());
              }
              else{ 
              	break; 
              }//繼續往下找尋符合圖片RE的字串
            }//結束找尋圖片
            
            
            }//沒找到停止條件
        }
          
   
       	fwriter.close();//關閉檔案
       	
        }catch (IOException e){
        	e.printStackTrace();
        }
    }//end function read1

	
        
}//end class search

